package result

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop/pkg/err"
)

type Content struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type Result struct {
	Ctx *gin.Context
}

func (r Result) Success(data interface{}) {
	res := Content{0, "", data}
	r.Ctx.JSON(http.StatusOK, res)
}

func (r Result) Error(e err.ErrorCode) {
	res := Content{e.Code, e.Msg, nil}
	r.Ctx.JSON(http.StatusOK, res)
	r.Ctx.Abort()
}

func Create(ctx *gin.Context) *Result {
	return &Result{Ctx: ctx}
}
